package com.gelsanalyzer.application;

import java.awt.*;
import java.beans.*;
import java.util.List;

import javax.swing.*;

import com.gelsanalyzer.business.*;
import com.gelsanalyzer.core.*;
import com.gelsanalyzer.mvc.controller.*;
import com.gelsanalyzer.mvc.view.*;

public class CourseSelectorView extends AbstractViewPanel {

    public CourseSelectorView() {
        initComponents();
        addComponents();
    }

    private void addComponents() {
        JComponent[][] comps = { { labelStudent, fieldName },
            { labelDepartment, comboDepartments },
            { labelCourse, comboCourseNums }, { buttonAddCourse, buttonRemoveCourse } };
        int[][] widths = { { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5 } };

        // addComponents(comps, widths, 8);

        setLayout(new GridLayout(7, 2, 5, 5));
        add(labelStudent);
        add(fieldName);
        add(labelDepartment);
        add(comboDepartments);
        add(labelCourse);
        add(comboCourseNums);
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(buttonAddCourse);
        add(buttonRemoveCourse);
    }

    private void initComponents() {
        labelStudent = new JLabel("Student Name");
        labelDepartment = new JLabel("Department");
        labelCourse = new JLabel("Course");

        fieldName = new JTextField();
        comboDepartments = new JComboBox();
        comboDepartments.setAction(ActionFactory.getCourseNums());
        comboCourseNums = new JComboBox();
        comboCourseNums.setAction(ActionFactory.getGetCourseEntryItemCommand());
        buttonAddCourse = new JButton(ActionFactory.getAddCourse());
        buttonRemoveCourse = new JButton(ActionFactory.getRemoveCourse());

        this.fieldName.setEnabled(false);

    }

    @SuppressWarnings(value= {"unchecked"})
    public void modelPropertyChange(PropertyChangeEvent event) {
        if ( event.getPropertyName().equals(
            IController.APP_DEPARTMENTS_PROPERTY) ) {
            List<Department> departments = (List<Department>) event
                .getNewValue();
            if ( departments == null ) {
                throw new NullPointerException(
                    "departments was null and shouldn't have been");
            } else {
                for ( Department department : departments ) {
                    comboDepartments.addItem(department);
                }
            }
        }else if(event.getPropertyName().equals(IController.APP_COURSES_SET)) {
            List<Course> courses = (List<Course>) event.getNewValue();
            
            if ( courses == null ) {
                throw new NullPointerException(
                    "courses was null and shouldn't have been");
            } else {
                comboCourseNums.removeAllItems();

                comboCourseNums.addItem(null);
                for ( Course course : courses ) {
                    comboCourseNums.addItem(course);
                }
                ActionFactory.getAddCourse().setEnabled(false);
                ActionFactory.getRemoveCourse().setEnabled(false);
            }
        }
    }

    private JLabel labelStudent;
    private JLabel labelDepartment;
    private JLabel labelCourse;

    private JTextField fieldName;
    private JComboBox comboDepartments;
    private JComboBox comboCourseNums;
    private JButton buttonAddCourse;
    private JButton buttonRemoveCourse;
}
